package com.example.demo;

import java.util.Date;
import java.util.Properties;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.lucene.queryparser.flexible.core.builders.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.dao.CityDao;
import com.example.demo.dao.HotelDao;
import com.example.demo.domain.City;
import com.example.demo.domain.Hotel;
import com.example.demo.service.TestService;
import com.youxin.busfoundation.config.RedisClusterConfig;
import com.youxin.busfoundation.config.RedisSentinelConfig;
import com.youxin.es.service.NewsService;
import com.youxin.foundation.db.redis.RedisClusterCommand;
import com.youxin.foundation.utils.ConfigurationUtils;
import com.youxin.kafka.KafkaConfig;
import com.youxin.kafka.KafkaProducerTask;

/**
 * @author jinwen.you
 * 把今天最好的表现当作明天最新的起点．．～
 * いま 最高の表現 として 明日最新の始発．．～
 * Today the best performance  as tomorrow newest starter!
 * Created by IntelliJ IDEA.
 *
 * author: xiaomo
 * github: https://github.com/xiaomoinfo
 * email: xiaomo@xiaomo.info

 * Date: 2016/11/16 10:45
 * Description: 用户实体类
 * Copyright(©) 2015 by xiaomo.
 **/

@RestController
public class MultipleSourceController {
    private final JdbcTemplate jdbcTemplate1;

    private final JdbcTemplate jdbcTemplate2;

    
    @Autowired
    private CityDao cityDao;
    
    @Autowired
    private HotelDao hotelDao;
    
    @Autowired
    @Qualifier("kafkaConfig")
    private KafkaConfig kafkaConfig;
    
    @Autowired
    ElasticsearchTemplate elasticsearchTemplate;
    
    @Autowired
    public MultipleSourceController(@Qualifier("primaryJdbcTemplate") JdbcTemplate jdbcTemplate1, @Qualifier("secondaryJdbcTemplate") JdbcTemplate jdbcTemplate2) {
        this.jdbcTemplate1 = jdbcTemplate1;
        this.jdbcTemplate2 = jdbcTemplate2;
      //  this.jdbcTemplate1.update(Sql.dropUser);
        //this.jdbcTemplate2.update(Sql.dropUser);
    }
    
    
    public void test() {
       // SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(queryStringQuery("spring boot OR 书籍")).build();
//        List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
//        for (Article article : articles) {
//            System.out.println(article.toString());
//        }
    }

    @RequestMapping("/kafka")
    public String testKafka() {
//    	try {
//        	RedisClusterCommand.initConfig(redisClusterConfig);
//        	RedisClusterCommand.set("RedisClusterCommand", "value");
//		} catch (Exception e) {
//			// TODO: handle exception
//		}
    	Properties properties=ConfigurationUtils.getProperties("kafka.properties");
    	//new java.lang.Thread(new KafkaProducerTask(properties, new ProducerRecord<String,String>("test", "testKey", "My Kafka:"+new Date().toString()))).start();;
    	return "Kafka";
	}

//    @Autowired
//    private RedisClusterConfig redisClusterConfig;
    
//    @Autowired
//    private RedisSentinelConfig redisSentinelConfig;
    
    @RequestMapping("/sql")
    public String index() {
    	int i=0;
    	++i;
    	City city=cityDao.selectCityById(1);
    	Hotel hotel=hotelDao.selectCityById(1);
        // 往第一个数据源中插入两条数据
        jdbcTemplate1.update(Sql.addUser,System.currentTimeMillis(),"xiaomo");
        jdbcTemplate2.update(Sql.addUser,System.currentTimeMillis(),"xiaoming");
        
        int count1 = jdbcTemplate1.queryForObject(Sql.selectUser, Integer.class);
        int count2 = jdbcTemplate2.queryForObject(Sql.selectUser, Integer.class);
        return "sql";
    }
}
